#' 增加steiger结果
#'
#' @param res U5_mr后的res
#' @param dat U4_harmonise_data后的dat
#' @param workers 多项目同时运行，加速分析，默认为1。
#'
#' @return res
#' @export
#'
#' @examples
#'
#' \dontrun{
#'
#' library(Oneclick)
#'
#' res <- U5_mr(dat)
#'
#' res <- res %>% U5_add_steiger(dat)
#'
#'
#' }
#'
#'
#'
#'
#'
#'
#'
#'
#'
#'
#'
#'
U5_add_steiger<-function(res=res,dat=dat, workers=1){

  message( "清理数据......" )

  dat$units.outcome<- ifelse( dat$type.outcome=='Binary',"log odds","SD"  )
  dat$units.exposure<- ifelse( dat$type.exposure=='Binary',"log odds","SD"  )

  suppressMessages(dat <- U4_add_eaf(dat, what = "exposure" )  )
  suppressMessages(dat <- U4_add_eaf(dat, what = "outcome" )  )
  suppressMessages(dat <- U4_check_samplesize(dat )  )

  dat <- subset(dat, mr_keep) %>%
    split(list( .$id.exposure , .$id.outcome) )

  dat <- Filter(function(x){ nrow(x) > 0} , dat)

  future::plan( future::multisession, workers = workers )

  message( "增加steiger结果......" )
  direction <- dat  %>%
    furrr::future_map_dfr( add_steiger,.options = furrr::furrr_options(seed = TRUE),.progress = TRUE
    )
  mr_tab <- dplyr::full_join(res,direction, by = c('id.exposure','id.outcome'))
  return(mr_tab)
}


add_steiger <- function(dat){
  require(TwoSampleMR)

  dat <- subset(dat, mr_keep==TRUE)
  dat <- dat %>% dplyr::distinct(SNP,.keep_all = TRUE)

  suppressWarnings( dat<- add_rsq(dat) )

  dat$r.exposure<-dat$rsq.exposure
  dat$r.outcome<-dat$rsq.outcome

  direction<- directionality_test(dat)[,-(3:4)]

  return(direction)
}
